﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using MySQLDriverCS;
using SafeDoorService;
using ServiceConfigTool; 

namespace DAL
{
    public class DAL
    {
        private static string lastError;

        /// <summary>
        /// 错误信息
        /// </summary>
        public static string LastError
        {
            get { return DAL.lastError; } 
        }
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static readonly string LocalDbName = "LocalDB"; 

        /// <summary>
        /// 通过RFID获取条码(使用RFID的16进制UID，4个word)
        /// </summary>
        /// <returns></returns>
        public static List<string> GetBarcode(string[] rfids)
        { 
            List<string> list = new List<string>();
            if (null == rfids || rfids.Length < 1)
                return list;
            MySQLConnection conn = new MySQLConnection(clsCommon.GetConnStr(ConfigurationManager.AppSettings[LocalDbName]));
            try
            {
                conn.Open();
            }
            catch (MySQLException)
            {
                return list;
            }
            // 处理中文乱码
            MySQLCommand cmd = new MySQLCommand("set names gb2312", conn); 
            try
            {
                cmd.ExecuteNonQuery();

                foreach (string rfid in rfids)
                {
                    string sql = "SELECT barcode from bookinfo where LEFT(rfid,16) ='" + rfid + "' ORDER BY barcode LIMIT 0,1";
                    cmd = new MySQLCommand(sql, conn);
                    object barcode = cmd.ExecuteScalar();
                    if (barcode != null)
                        list.Add(barcode.ToString());
                } 
                return list;
            }
            catch (MySQLException)
            {
                list.Clear();
                return list;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
        }


        /// <summary>
        /// 查看图书的借阅状态 0 未借 1 借出
        /// </summary>
        /// <returns></returns>
        public static int CheckIsBorrowed(string rfid)
        {
            int iRes = -1;
            if (string.IsNullOrEmpty(rfid))
            {
                lastError = "rfid为空";
                return iRes;
            }
            MySQLConnection conn = new MySQLConnection(clsCommon.GetConnStr(ConfigurationManager.AppSettings[LocalDbName]));
            try
            {
                conn.Open();
            }
            catch (MySQLException ex)
            {
                lastError = "打开网络失败:"+ex.Message;
                return iRes;
            }
            // 处理中文乱码
            MySQLCommand cmd = new MySQLCommand("set names gb2312", conn);
            try
            {
                cmd.ExecuteNonQuery();
                string sql = "SELECT isBorrowed from bookinfo where rfid ='" + rfid + "' LIMIT 0,1";
                cmd = new MySQLCommand(sql, conn);
                object isBorrowed = cmd.ExecuteScalar();
                if (null != isBorrowed)
                    iRes = Convert.ToInt32(isBorrowed);
                else
                    iRes = 1;
                return iRes;
            }
            catch (MySQLException ex)
            {
                lastError = "查询出现异常："+ex.Message;
                iRes = -1;
                return iRes;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
        }
         
    }
}